package com.sg.service.base.dataassets.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.dataassets.req.QueryAlbumShareRecListReq;
import com.sg.entity.OmsAlbumShareRec;
import com.sg.service.base.dataassets.MOmsAlbumShareRecService;
import com.wicket.okrdataassets.common.dto.FieldInfo;
import com.wicket.okrdataassets.common.exception.BizException;
import com.wicket.okrdataassets.dal.mapper.mbg.OmsAlbumShareRecMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAlbumShareRecServiceImpl
        extends ServiceImpl<OmsAlbumShareRecMapper, OmsAlbumShareRec>
        implements MOmsAlbumShareRecService {

    @Autowired
    OmsAlbumShareRecMapper omsAlbumShareRecMapper;

    /**
     * 8-02-03新增专辑分享记录表[10309]
     * gen by moon at 11/11/2024, 1:24:56 AM
     */
    @Trace(operationName = "8-02-03新增专辑分享记录表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAlbumShareRec(OmsAlbumShareRec omsAlbumShareRec) {
        omsAlbumShareRec.setAlbumShareRecId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAlbumShareRec.setSubjectLifeCycle("CURRENTLY_VALID");
        omsAlbumShareRec.setIsArchive("FALSE");
        omsAlbumShareRec.setIsValid("FALSE");
        omsAlbumShareRec.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbumShareRec.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumShareRec.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAlbumShareRec.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbumShareRec.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbumShareRec.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAlbumShareRec.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumShareRec.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsAlbumShareRec.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAlbumShareRec.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAlbumShareRec)) {
            return omsAlbumShareRec.getAlbumShareRecId();
        } else {
            return "-1";
        }

    }

    /**
     * 8-02-03修改专辑分享记录表[10310]
     * gen by moon at 11/11/2024, 1:24:57 AM
     */
    @Trace(operationName = "8-02-03修改专辑分享记录表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAlbumShareRec(OmsAlbumShareRec omsAlbumShareRec) {
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumShareRec.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAlbumShareRec.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbumShareRec.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbumShareRec != null && omsAlbumShareRec.getSubjectLifeCycle() != null && omsAlbumShareRec.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumShareRec.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAlbumShareRec);
    }

    /**
     * 8-02-03查询专辑分享记录表列表[10311]
     * gen by moon at 11/11/2024, 1:25:00 AM
     */
    @Trace(operationName = "8-02-03查询专辑分享记录表列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAlbumShareRec> queryAlbumShareRecList(QueryAlbumShareRecListReq reqDto) {

        try {
            QueryWrapper<OmsAlbumShareRec> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAlbumShareRec.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAlbumShareRec.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAlbumShareRec.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAlbumShareRec>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
